/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */

package org.ipig.constants;

/**
 * CacheCnst
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: CacheCnst.java 12 2020-04-15 11:35:15Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public class CacheCnst {
    public static final String CACHE_TYPE = "cacheType";
    public static final String CACHE_PROVIDER = "cacheProvider";
    public static final String CACHE_SIZE = "cacheSize";
    public static final String CACHE_TIME_SECONDS = "cacheTimeSeconds";
    public static final String CACHE_CONF = "conf";

    /**
     * Cache ExpiryPolicy
     */
    public enum ExpiryPolicy {
        /**
         * 缓存中的条目应在定义的时间之后过期
         */
        TTL("TTL", "缓存中的条目应在定义的时间之后过期"),
        /**
         * 在定义的时间内未访问，则缓存中的条目应过期
         */
        TTI("TTI", "在定义的时间内未访问，则缓存中的条目应过期");
        //type
        public final String code;
        //title
        public final String title;

        /**
         * @param code
         * @param title
         */
        ExpiryPolicy(String code, String title) {
            this.code = code;
            this.title = title;
        }

        /**
         * 解析ExpiryPolicy
         *
         * @param code 编码
         * @return ExpiryPolicy
         */
        public static ExpiryPolicy parse(String code) {
            for (ExpiryPolicy policy : ExpiryPolicy.values()) {
                if (policy.code.equals(code)) {
                    return policy;
                }
            }
            return TTI;
        }
    }

    /**
     * Cache Provider
     */
    public enum CacheProvider {
        /**
         * 混合ehcache缓存支持堆内存，直接内存，磁盘，集群几种组合
         */
        HYBRID_EHCACHE_CACHE("ehcacheCache",
                "org.ipig.commons.cache.ehcache.EhcacheProvider",
                "ehcache混合cache"),
        /**
         * spike集群cache
         */
        CLUSTER_SPIKE_CACHE("clusterSpikeCache",
                "org.ipig.commons.cache.spike.SpikeCacheProvider",
                "spike集群cache"),
        /**
         * 本地同步LRU cache
         */
        LOCAL_SYNCHRONIZED_LRU_CACHE("localSynchronizedLRUCache",
                "org.ipig.commons.cache.local.LocalLRUCacheProvider",
                "本地同步LRU cache"),
        /**
         * 本地并发LRU cache
         */
        LOCAL_CONCURRENT_LRU_CACHE("localConcurrentLRUCache",
                "org.ipig.commons.cache.local.LocalLRUConcurrentCacheProvider",
                "本地并发LRU cache");
        //type
        public final String type;
        //provider
        public final String provider;
        //title
        public final String title;

        /**
         * @param type
         * @param provider
         * @param title
         */
        CacheProvider(String type, String provider, String title) {
            this.type = type;
            this.provider = provider;
            this.title = title;
        }

        /**
         * 解析CacheProvider
         *
         * @param type 编码
         * @return CacheProvider
         */
        public static CacheProvider parse(String type) {
            for (CacheProvider provider : CacheProvider.values()) {
                if (provider.type.equals(type)) {
                    return provider;
                }
            }
            return LOCAL_CONCURRENT_LRU_CACHE;
        }
    }
}
